$(document).ready(function () {
    // 点击发送验证码开始
    var onOff = 0;//控制发送验证码开关
    $('.verification').on('click',function () {
        if(onOff==0){
            $.ajax({
                "url": "/sendValidateCode",
                "type": "POST",
                data: {
                    "bizType": "USER_INFO_MODIFY"
                },
                success: function (result) {
                    if (result.code == 1) {
                        //按钮30s秒倒计时
                        var time = 30;
                        var Inter = setInterval(function () {
                            if (time >= 1) {
                                time--;
                                $(".verification").html(time + "s后重新发送");
                                $(".verification").css("font-size",'0.6rem');
                                onOff=1;
                            } else {
                                clearInterval(Inter);
                                $(".verification").html("重新发送");
                                $(".verification").css("font-size","0.75rem");
                                onOff=0;
                            }
                        }, 1000);


                    } else {
                        $.toast(result.message);
                    }
                }
            });
        }

    });
    // 点击发送验证码结束

    // 发起提现开始
    $('.fq_withdrawal').on('click',function () {
        // 获取提现信息
        var amount = $('.amount').val();
        var validateCode = $('.validateCode').val();
        var  pageData= {
            amount : amount,
            validateCode : validateCode
        };
        // 判断提现信息是否有误
        if(validateCode==''){
            $.toast("请输入验证码");
        }else if(amount>=2){
            if(/^\d+(\.\d{1,2})?$/.test(amount)){
                $.post('/withdrawManage/withdraw',pageData,function (result) {
                    if(result.code == 1){
                        $.toast("提现成功");
                        $('.validateCode').val('');
                        window.location.href='/withdrawManage/list';
                    }else{
                        $.toast(result.message);
                        $('.validateCode').val('');
                    }
                });
            }else {
                $.toast("只能保留两位小数");
            }
        }else {
            $.toast("金额太少,不能提现");
        }
    });
    // 发起提现结束


    // 选择查询类型开始
    var i = 0; //控制点击切换
    $('.withdrawal_query_val').on('click',function () {
        if(i==0){
            $('.withdrawal_query_list ul').show();
            i=1;
        }else {
            $('.withdrawal_query_list ul').hide();
            i=0;
        }
    });
    $('.withdrawal_query_list li').on('click',function () {
        var type = $('.withdrawal_query_list li').eq($(this).index()).html();
        var typeVal = $('.withdrawal_query_list li').eq($(this).index()).attr('value');
        $('.withdrawal_query_val').html(type);
        $('.withdrawal_query_val').attr('value',typeVal);
        $('.withdrawal_query_list ul').hide();
        if($(this).index()==0){
            $('.withdrawal_query_val').attr('value','');
        }
        i=0;
    });
    // 选择查询类型结束

    // 进入页面全部加载
    window.onload = function () {
        init_datalist();
    };

    // 点击查询开始
    $('.withdrawal_query_but').on('click',function () {
        init_datalist();
    });
    // 点击查询结束


    // 下拉刷新开始

    var falg=true;

    var pageNo=1;

    var pageSize=10;
    // 上次加载的序号
    var lastIndex = 0;
    // 加载flag
    var loading = false;

    //最大加载数量
    var maxItems=0;

    var removeFlag=false;


    // var startTime = new Date().Format("yyyy-MM-dd");

    $.initPullToRefresh(".pull-to-refresh-content"); //- 初始化/启用下拉刷新

    // 添加'refresh'监听器

    $(document).on('refresh', '.pull-to-refresh-content',function(e) {

        if (loading) {
            return;
        }
        // 设置flag
        loading = true;

        setTimeout(function () {
            init_datalist();
        },500);

    });

    function init_datalist() {
        pageNo=1;
        $('.cards-list .card').remove();
        addItems(1,pageSize);
        $.pullToRefreshDone('.pull-to-refresh-content');//重置特定的下拉刷新界面
    }

    //刷新页面
    function refreshPage(pageNo, pageSize){
        addItems(pageNo, pageSize);
    }

    // 数据请求函数开始
    function addItems(pageNo, pageSize) {
        var  param =  {
            Succeed: $('.withdrawal_query_val').attr('value'),
            pageNo:pageNo,
            pageSize:pageSize

        };
        // 生成新条目的HTML
        if(falg){
            falg=false;
            $.ajax({
                "url": "/withdrawManage/queryData",
                "type": "POST",
                async:false,
                data:param,
                success:function (result) {
                    falg=true;
                    maxItems=result.recordsTotal;

                    var data=result.data;

                    if(data.length==0){

                        $(".no-data").css('display','block');
                        return;
                    }else{
                        $(".no-data").css('display','none');
                    }
                    for(var i=0;i<data.length;i++) {
                        var withdrawData =data[i];
                        var html = '';
                        var succeedType = '';
                        if(withdrawData.succeed=="-11"){
                            succeedType = "正在处理中";
                        }else if(withdrawData.succeed=="00"){
                            succeedType = "提现成功";
                        }else{
                            succeedType = "转账退回";
                        }
                        if(withdrawData.actualAmount==null||withdrawData.actualAmount==0){
                            withdrawData.actualAmount=withdrawData.amount;
                        }
                        html+= '<li class="card">' +
                            '<div class="card-header">'+
                            '<i class="iconfont icon-yonghu" style="font-size: 1.5rem"></i>'+
                            '<span style="vertical-align: middle;color:#0894ec">'+
                            withdrawData.accountName+
                            '</span>'+
                            '</div>' +
                            '<div class="card-content">' +
                            '<div class="item-content">'+
                            '<div class="item-media">'+
                            '<img src="/image/yinhangka.png" width="30px"/>'+
                            '</div>'+
                            '<div class="item-inner">'+
                            '<div class="item-title-row">'+
                            '<div class="item-title">'+withdrawData.cardNo+'</div>'+
                            '</div>'+
                            '</div>'+
                            '</div>'+
                            '</div>' +
                            '<div class="card-footer">'+
                            '<span>'+'类型:'+succeedType+'</span>'+
                            '</div>'+
                            '<div class="card-footer">'+
                            '<span style="color: #ff2400">'+'提现金额:'+withdrawData.amount/100+'</span>'+
                            '<span style="color: #FFB90F">'+'税率及费用:'+withdrawData.cost/100+'</span>' +
                            '</div>' +
                            '<div class="card-footer">'+
                            '<span style="color: #4CD864">'+'扣费后金额:'+withdrawData.actualAmount/100+'</span>'+
                            '<span>'+'提现时间:'+withdrawData.createdTime+'</span>' +
                            '</div>'+
                            '</li>';

                        $('.cards-list ul').append(html);
                    }
                    lastIndex = $('.cards-list .card').length;
                    removeFlag=false;
                    loading = false;
                }
            })
        }
    }
    // 数据请求函数结束
    // 注册'infinite'事件处理函数
    $(document).on('infinite', '.infinite-scroll-bottom',function(e) {
        // 如果正在加载，则退出
        if (loading) {
            return;
        }
        // 设置flag
        loading = true;
        if (lastIndex >= maxItems) {

            if (maxItems != 0) {
                $('.infinite-scroll-preloader').hide();
                loading = false;
                $.toast("已经到底了");
            }
            return;
        }
        pageNo++;
        $('.infinite-scroll-preloader').show();
        setTimeout(function () {
            refreshPage(pageNo,pageSize);
            $('.infinite-scroll-preloader').hide();
            $.refreshScroller();
        },500)
    });
    // 下拉刷新结束
});